/*
 * Copyright (c) 2015 Uwantsoft Ltd.
 * www.uwantsoft.com
 * All rights reserved.
 * This software is the confidential and proprietary information of
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with Uwantsoft.
 */

package com.uwantsoft.interfaces.web.controller.web;

import com.uwantsoft.business.facade.CityFacadeImpl;
import com.uwantsoft.business.facade.dto.city.CityDetailDto;
import com.uwantsoft.business.facade.dto.city.CityFormDto;
import com.uwantsoft.business.facade.dto.parking.CityOverviewQueryCondition;
import com.uwantsoft.util.paging.DefaultPaginatedList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author Leipeichen.
 */
@Controller
@RequestMapping(value = "/backend")
public class CityController {

    @Resource(name = "cityFacade")
    private CityFacadeImpl cityFacade;

    @RequestMapping(value = "cityoverview")
    public String obtainCities(CityOverviewQueryCondition condition,Model model){
        DefaultPaginatedList cityPaginatedList = cityFacade.obtainCities(condition);
        model.addAttribute("cityPaginatedList",cityPaginatedList);
        return "backend/cityoverview";
    }
    @RequestMapping(value = "cityform",method = RequestMethod.POST)
    public String addOrUpdate(@Valid CityFormDto cityFormDto,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return "backend/cityform";
        }
        cityFacade.addOrUpdate(cityFormDto);
        return "redirect:cityoverview";
    }
    @RequestMapping(value = "city")
    public String obtainCity(String guid,Model model){
        CityFormDto cityFormDto = cityFacade.obtainCity(guid);
        model.addAttribute("cityFormDto",cityFormDto);
        return "backend/cityform";
    }

    @RequestMapping(value = "citydetail")
    public String obtainCityDetail(String guid,Model model){
        CityDetailDto cityDetailDto = cityFacade.obtainCityDetail(guid);
        model.addAttribute("cityDetailDto", cityDetailDto);
        return "backend/citydetail";
    }
}
